<?php
namespace Home\Controller;
use Think\Controller;
use Common\Library\Page;
class ArticleController extends Controller{
	private $Model;
	private $CateModel;
	function _initialize(){
		$this->Model=D('Article');
		$this->CateModel=D('ArticleCategory');
	}
	public function index($article_category_id=0){
		//如果传入的$article_category_id小于0或者不是整数，表示参数不正确
		if(!is_numeric($article_category_id) || (int)$article_category_id<1) $this->error(L('error_404'));
		//获取分类及父级分类
		$breadcrumb=$this->CateModel->getParents($article_category_id);
		//如果分类不存在404提示
		if(empty($breadcrumb)) $this->error(L('error_404'));
		
		$category=$breadcrumb[count($breadcrumb)-1];

		$general=array(
			'page'	=>	(int)C('ARTICLE_PAGE_LIMIT',null,'24'),
			'width'	=>	0,
			'height'=>	0
			);
		foreach ($breadcrumb as &$value){
			if($value['list_tpl']) $general['list_tpl']=$value['list_tpl'];
			if((int)$value['width']>-1) $general['width']=$value['width'];
			if((int)$value['height']>-1) $general['height']=$value['height'];
			if((int)$value['page']>-1) $general['page']=$value['page'];
		}
		unset($value);
		$filter=array('article_category_id'=>(int)$article_category_id);
		$count=$this->Model->getCount($filter);
		$Page=new Page($count,(int)$general['page']);
		$Page->setConfig('header',L('page_header'));
		$Page->setConfig('theme',L('page_theme'));
		$show=$Page->show();
		$filter['start']=$Page->firstRow;
		$filter['limit']=$Page->listRows;
		$list=$this->Model->getArticles($filter);
		foreach ($list as &$v){
			$v['thumb']=image_resize($v['image'],$general['width'],$general['height']);
		}
		unset($v);
		$data=array(
			'category'	=>	$category,
			'breadcrumb'=>	$breadcrumb,
			'list'		=>	$list,
			'page'		=>	$show,
			'meta_title'=>	$category['meta_title'],
			'meta_description'=>$category['meta_description'],
			'meta_keyword'=>	$category['meta_keyword']
			);
		$this->assign($data);
		$this->display($general['list_tpl']);
	}

	public function detail($article_id=0){
		//如果传入的$article_id小于0或者不是整数，表示参数不正确
		if(!is_numeric($article_id) || (int)$article_id<1) $this->error(L('error_404'));
		$article=$this->Model->getArticle($article_id);
		//如果为空，显示页面不存在
		if(empty($article)) $this->error(L('error_404'));
		
		$breadcrumb=$this->CateModel->getParents($article['article_category_id']);
		//如果分类不存在
		if(empty($breadcrumb)) $this->error(L('error_article_exist'));

		$where=array('article_id'=>$article_id);
		//更新点击量
		$this->Model->where($where)->setInc('viewed',1);

		if(!$article['template']){
			//如果文章本身未写模板，使用分类的“详细页面模板”
			foreach($breadcrumb as &$v){
				if($v['detail_tpl']) $template=$v['detail_tpl'];
			}
			unset($v);
		}else{
			//如果文章填写了模板，则使用文章自身的模板
			$template=$article['template'];
		}
		$info=$article['article_description'];
		$info['content']=htmlspecialchars_decode($info['content']);
		unset($article['meta_description']);
		//相关内容
		$relateds=M('ArticleRelated')->where(array('article_id'=>$article_id))->select();
		foreach ($relateds as $key => $value) {
			$related_info=$this->Model->getArticle($value['related_id']);
			$article['article_related'][]=array(
				'article_id'	=>	$value['related_id'],
				'url'			=>	U('Article/detail?article_id=' . $value['related_id']),
				'date'			=>	date('Y-m-d',$related_info['date_modified']),
				'viewed'		=>	$related_info['viewed'],
				'title'			=>	$related_info['article_description']['title'],
				'meta_description'=>$related_info['article_description']['meta_description']
				);
		}
		$data=array(
			'meta_title'	=>	$info['meta_title'],
			'meta_description'=>$info['meta_description'],
			'meta_keyword'	=>	$info['meta_keyword'],
			'info'			=>	$info,
			'article'		=>	$article
			);
		$this->assign($data);
		$this->display($template);
	}
}